package com.dingding.gmall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.Query;
import com.dingding.gmall.common.utils.R;
import com.dingding.gmall.ware.dao.WareInfoDao;
import com.dingding.gmall.ware.dto.MemberAddressDTO;
import com.dingding.gmall.ware.entity.WareInfoEntity;
import com.dingding.gmall.ware.feign.MemberFeignService;
import com.dingding.gmall.ware.service.WareInfoService;
import com.dingding.gmall.ware.vo.FareInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.eq("id",key).or()
                    .like("name",key).or()
                    .like("address",key).or()
                    .like("areacode",key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }


    @Override
    public FareInfoVO getFare(Long addressId) {
        R r = memberFeignService.info(addressId);
        FareInfoVO fareInfoVO = new FareInfoVO();
        if (r != null) {
            MemberAddressDTO memberAddressDTO =  r.getData("memberReceiveAddress",new TypeReference<MemberAddressDTO>(){});
            String phone = memberAddressDTO.getPhone();
            String fare = phone.substring(phone.length()-1, phone.length());
            fareInfoVO.setAddressId(addressId);
            fareInfoVO.setFare(new BigDecimal(fare));
        }
        return fareInfoVO;
    }
}